package com.example.myblog.service.impl;

import com.example.myblog.dao.CommentDao;
import com.example.myblog.pojo.Comment;
import com.example.myblog.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @BelongsProject: MyBlog
 * @BelongsPackage: com.example.myblog.service.impl
 * @ClassName CommentServiceImpl
 * @CreateTime: 2025-06-10  09:46
 * @Author: TangLiFei
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;

    @Override
    public List<Comment> findAll() {
        List<Comment> comments=commentDao.findAll();
        return comments;
    }

    @Override
    public void update(Comment comment) {
        // 业务逻辑：更新时刷新修改时间
        comment.setCreated(new java.util.Date());
        commentDao.update(comment);
    }

    @Override
    public PageInfo<Comment> getComments(Integer aid, int page, int count) {
        PageHelper.startPage(page, count);
        List<Comment> comments = commentDao.selectCommentWithPage(aid);
        return new PageInfo<>(comments);
    }

    @Override
    public void pushComment(Comment comment) {
        try {
            commentDao.pushComment(comment);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deleteCommentById(Integer id) {
        try {
            commentDao.deleteCommentById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
